package kr.hyunist.memoappproject;

import java.text.SimpleDateFormat;
import java.util.*;

import kr.hyunist.memoappproject.BaseAdapter.*;
import kr.hyunist.memoappproject.DAO.*;
import kr.hyunist.memoappproject.DTO.*;
import kr.hyunist.memoappproject.SQLiteHelper.MyMemoAppSqlHelper;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class MemoWrite extends Activity {
	EditText contentEt;
	EditText titleEt;
	MyMemoAppAdapter Adapter;
	
	Calendar cal;
	Date day;
	SimpleDateFormat sdf;	
	
	MyMemoAppSqlHelper mHelper;
	MyMemoAppDAO Dao;

	boolean result;
	boolean updateResult;
	
	int idOk;
	String messageOk;
	String titleOk;
	
	Intent intent = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memowrite);

		cal = Calendar.getInstance();
		day = cal.getTime();
		sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
		
		contentEt = (EditText) findViewById(R.id.memoappwriteet);

		mHelper = new MyMemoAppSqlHelper(MemoWrite.this);
		Dao = MyMemoAppDAO.open(MemoWrite.this);
		
		//리스트에서 id와 내용을 받아옴
		Intent intent = getIntent();
		idOk = intent.getIntExtra("IdIn", 1);
		titleOk = intent.getStringExtra("TitleIn");
		messageOk = intent.getStringExtra("ContentIn");
		if(messageOk != null){
			contentEt.setText(messageOk);
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.actionbarmenuupdate, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		titleEt = new EditText(this);
		
		if(messageOk != null){
			titleEt.setText(titleOk);
		} else {
			titleEt.setHint("Default");
		}
		
		switch (item.getItemId()) {
		case R.id.actionbarupdatebtn:
			AlertDialog.Builder dlg = new AlertDialog.Builder(MemoWrite.this);
			dlg.setTitle("제목을 입력하세요");
			dlg.setIcon(R.drawable.memoicon);
			dlg.setView(titleEt);
			dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					String res = null;
										
					if(messageOk != null){
						result = Dao.updateMemo(idOk, titleEt.getText().toString(), contentEt.getText().toString(), String.valueOf(sdf.format(day)));
					} else {
						result = Dao.insertMemo(titleEt.getText().toString(), contentEt.getText().toString(), String.valueOf(sdf.format(day)));
					}
					
					intent = new Intent(MemoWrite.this, MainActivity.class);
					if (result){						
						setResult(RESULT_OK, intent);
						res = "입력성공!";
					} else {
						setResult(RESULT_CANCELED, intent);
						res = "입력실패!";
					}
					
					Toast.makeText(MemoWrite.this, res, Toast.LENGTH_SHORT).show();
					
					finish();
				}

			});

			dlg.show();
			break;
		}
		return true;
	}

}
